import Vue from 'vue'
import vuex from 'vuex'
import {query} from '../servers';
import FROMURL from '../exportURL';
import router from '../router/index';

Vue.use(vuex);
//引入modules
import xiaoma from './xiaoma';
import xiongxiaokang from './xiongxiaokang';
//基础应用数据

//路由白名单
const urlconfig = {
    '/':'',
    '/postMen':''
};
export default new vuex.Store({

    modules: {
        account: {
            namespaced: true,
            state: {
                err: false,
            },
            //过滤数据返回多个组件都需要的数据
            getters: {
                Example(state){
                    return state = state;
                },
            },
            //同步state函数
            mutations: {
                //直接把数据提交到state
                save(state, action){
                    console.log(action);
                    action.map(val=>{state[val[0]] = val[1]});
                },
                //错误代码返回
                errsave(state, val){
                    state.err = true;
                },
            },
            //异步函数
            actions: {
                //处理所有后台返回异常状态码
                async ErrCode(context, val){
                    if (val.payload == 200) {
                        return
                    }
                    console.log('错误代码:', val.payload)
                },
                //路由守卫
                async isloginState(state){
                    if(urlconfig[location.pathname]){return}else {
                        const {data} = await query(FROMURL.islogin,null,'get','obj');
                        console.log(data.title);
                        if (data.title=="登录成功"){return}{router.push({name:'login'})}
                    }
                },
                async Example(context, val){
                    // const {data} = await query(FROMURL.url,null,'get');
                    // console.log('数据:',data)
                    // val[1].$router.push({name:'home'});跳转路由
                    // context.commit('save',1111);
                },

            },
            //各组件store注册
            modules: [xiaoma,xiongxiaokang]
        }
    }
})
import Store from './store';
Store.dispatch({type:'account/isloginState'});
//监听浏览器前进后退事件
window.addEventListener('popstate', () => {
    //触发函数
    location.reload(true)
});